<?php
/**
 * Created by PhpStorm.
 * User: wx:wenyi2048
 * Date: 2022/3/18 0018
 * Time: 09:20
 */

namespace app\admin\controller\mk;
/*商品管理*/
\think\Loader::import('controller/Controller', \think\Config::get('traits_path'), EXT);

use app\admin\Controller;
use app\common\service\GoodsService;
use app\common\service\StoreGoodsFirstService;
use app\common\service\StoreTypeService;
use app\common\service\TypeService;
use think\Db;
use think\Exception;

class Goods extends Controller
{
    use \app\admin\traits\controller\Controller;

    /**
     * @return string
     * @throws \think\Exception 重写controller index方法
     */
    public function index(){

        $param = $this->request->param();
        $service = new GoodsService();//数据处理
        $service->searchPage($this->view,$param);


        $sms_stores_id = getAdminStoreId($this->request->param('store_id'));
        $storeTypeService = new StoreTypeService();
        $storeTypes = $storeTypeService->getStoreType($sms_stores_id);
        $this->view->assign('storeTypes',$storeTypes);

        return $this->view->fetch();

    }

    public function top(){
        $param = $this->request->param();
        $service = new GoodsService();//数据处理
        $service->topList($this->view,$param);
        return $this->view->fetch();
    }

    public function topAdd(){
        $param = $this->request->param();
        $service = new GoodsService();//数据处理
        try {
            if($service->topAdd(@$param['gIds'])){
                return  apiSuccess();
            }
            return ajax_return_adv('添加失败');
        }catch (Exception $e){
            return ajax_return_adv($e->getMessage());
        }
    }

    public function topDelete(){
        $param = $this->request->param();
        $service = new GoodsService();//数据处理
        if(empty($param['id'])) return ajax_return_adv_error('请先选择');
        if($service->topDelete(@$param['id'])){
            return ajax_return_adv_error('删除失败', '');
        }
        return ajax_return_adv('删除成功', '');
    }

    public function recycleBin()
    {
        $param = $this->request->param();
        $service = new GoodsService();//数据处理
        $service->searchPage($this->view,$param,1);

        $sms_stores_id = getAdminStoreId($this->request->param('store_id'));
        $storeTypeService = new StoreTypeService();
        $storeTypes = $storeTypeService->getStoreType($sms_stores_id);
        $this->view->assign('storeTypes',$storeTypes);

        return $this->view->fetch();
    }

    public function beforeAdd(){
        //$typeService = new TypeService();
        $storeTypeService = new StoreTypeService();
        //$typeTree = $typeService->treeList();
        $sms_stores_id = getAdminStoreId($this->request->param('sms_stores_id'));
        $storeTypes = $storeTypeService->getStoreType($sms_stores_id);
        //$this->view->assign('typeTree',$typeTree);
        $this->view->assign('storeTypes',$storeTypes);
        $this->view->assign('sms_stores_id',$sms_stores_id);
    }

    public function afterEdit(){
        $this->beforeAdd();
//        $vo = $this->view->__get('vo');
//        $tfid = $vo['types_first_id'];
//        $tsid = $vo['types_second_id'];
//        $ttid = $vo['types_third_id'];
//        $types = (new TypeService())->getTypes([$tfid,$tsid,$ttid]);
//        $vo['types_name'] = $types[0]['name'].'/'.$types[1]['name'].'/'.$types[2]['name'];
//        $this->view->assign('vo', $vo);
    }

    protected function overAdd($ret){
        //添加或修改第一个分类第一个商品到商家关联表
        $storeId = $ret['sms_stores_id'];
        $service = new StoreGoodsFirstService();
        $service->addUpdateFun($storeId);
    }

    /**
     * 用于弹框中商品选择
     */
    public function select(){
        $param = $this->request->param();
        $service = new GoodsService();//数据处理
        $datalist = $service->searchPage($this->view,$param);
        //return ajax_return($datalist);
        return $this->view->fetch();
    }


}